package com.weeho.dongyong.controller;

import com.weeho.dongyong.entity.Approval;
import com.weeho.dongyong.entity.Task;
import com.weeho.dongyong.entity.TaskDetail;
import com.weeho.dongyong.service.ApprovalService;
import com.weeho.dongyong.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by moods on 17-1-1.
 */
@Controller
public class TaskController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private ApprovalService approvalService;


    @RequestMapping("/addTask")
    public String welcome(ModelMap model, HttpServletRequest request, Task task, TaskDetail taskDetail) {


        String taskId = taskService.addTask(task, new ArrayList<>());
        model.put("approval", approvalService.getApprovalByTask(taskId));
        return "indexTask";
    }

    /**
     * 查询完成或未完成审核的任务
     * @param modelMap
     * @param request
     * @return
     */
    @RequestMapping(value="/task",method = RequestMethod.GET)
    public String task(ModelMap modelMap, HttpServletRequest request) throws Exception{
        String finish = request.getParameter("finish");
        String userId = request.getParameter("userId");

        List<Approval> approvals;
        if (Boolean.valueOf(finish)) {
            // finish
            approvals = approvalService.getFinishTask(userId);
        } else {
            // not finish
            approvals  = approvalService.getUnFinishTask(userId);
        }

        // sort by date
        modelMap.put("list",taskService.sort(approvals));
        return "index";
    }

}
